package com.ruoyi.history.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.history.domain.History;
import com.ruoyi.history.service.IHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 药物使用历史Controller
 * 
 * @author ruoyi
 * @date 2025-04-12
 */
@RestController
@RequestMapping("/history/history")
public class HistoryController extends BaseController
{
    @Autowired
    private IHistoryService historyService;

    /**
     * 查询药物使用历史列表
     */
    @PreAuthorize("@ss.hasPermi('history:history:list')")
    @GetMapping("/list")
    public TableDataInfo list(History history)
    {
        startPage();
        List<History> list = historyService.selectHistoryList(history);
        return getDataTable(list);
    }

    /**
     * 导出药物使用历史列表
     */
    @PreAuthorize("@ss.hasPermi('history:history:export')")
    @Log(title = "药物使用历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, History history)
    {
        List<History> list = historyService.selectHistoryList(history);
        ExcelUtil<History> util = new ExcelUtil<History>(History.class);
        util.exportExcel(response, list, "药物使用历史数据");
    }

    /**
     * 获取药物使用历史详细信息
     */
    @PreAuthorize("@ss.hasPermi('history:history:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(historyService.selectHistoryById(id));
    }

    /**
     * 新增药物使用历史
     */
    @PreAuthorize("@ss.hasPermi('history:history:add')")
    @Log(title = "药物使用历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody History history)
    {
        return toAjax(historyService.insertHistory(history));
    }

    /**
     * 修改药物使用历史
     */
    @PreAuthorize("@ss.hasPermi('history:history:edit')")
    @Log(title = "药物使用历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody History history)
    {
        return toAjax(historyService.updateHistory(history));
    }

    /**
     * 删除药物使用历史
     */
    @PreAuthorize("@ss.hasPermi('history:history:remove')")
    @Log(title = "药物使用历史", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(historyService.deleteHistoryByIds(ids));
    }
}
